#ifndef _CCGIS_PLUGIN_MANAGER_H_
#define _CCGIS_PLUGIN_MANAGER_H_

#include "afxtempl.h"

namespace ccgis{

const int CCGIS_PLUGIN_VERSION = 1;

class CRenderFactory;

class AFX_EXT_CLASS CPluginItem
{
public:
    CPluginItem(){}
    virtual ~CPluginItem(){}
    
    // Class related
    virtual CString getClassName() {return "CPluginItem";};
    virtual CPluginItem* createNew(){return NULL;}
	
    virtual CString getDescribe() = 0;
};

class MDllFile;

class AFX_EXT_CLASS CPluginManager
{
public:
	CPluginManager();
	~CPluginManager();
	CArray<MDllFile*, MDllFile*> m_aDlls;

	virtual int FindAndLoadDLLs(const CString &directory, const CString &FileMask, bool Init, bool RemoveOnFail, int nVersion = 1);
	virtual int LoadDll(CString Name, bool Init = true, bool RemoveOnFail = true, int nVersion = 1);
};

typedef CPluginManager* CPluginManagerPtr;

#define VERSIONFUNCNOTFOUND		-1
#define COUNTFUNCNOTFOUND		-2
#define	REGISTERFUNCNOTFOUND	-3
#define VERSIONDIFF				-4
  
// class to handle a single dll file containing one plugin
class AFX_EXT_CLASS MDllFile
{
protected:
    CPluginManagerPtr m_SysMan;
    
    HINSTANCE		m_DllInstance;
    CString			m_Filename;
    
    int				m_NumClasses;
    
public:
    MDllFile();
    virtual ~MDllFile();
    
    // Class functions
    virtual CString getClassName() {return "MDllFile";};
    
    int setFilename(CString Name);
    CString getFileName(){return m_Filename;}
    virtual int setPluginManager(CPluginManagerPtr SysMan);
    virtual int dllInit(int nVersion = 1);
    virtual int dllFinish();
	
	HINSTANCE getInstance() { return m_DllInstance; }
};

typedef int (*QueryVersionFunc)(long *Dest);
typedef int (*QueryDescFunc)(char *Dest);
typedef int (*QueryCountFunc)();
typedef int (*RegisterFunc)(CPluginManager *SysMan, int IDNo);

}

#endif//_CCGIS_PLUGIN_MANAGER_H_